package Com.HupochuanAdmin.Dal.Tab;

import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import Com.Hupochuan.Dal.DBManager.DBManager;
import Com.Hupochuan.Vo.MovieActor;

public class MovieActorTab {
	/*
	 * 本类私有对象
	 */
	private DBManager<MovieActor> dbManager = new DBManager<MovieActor>(MovieActor.class);
	
	/*
	 * 单一实例
	 */
	private MovieActorTab(){}
	private static MovieActorTab movieActorTab = new MovieActorTab();
	public static MovieActorTab getObject(){
		return movieActorTab;
	}
	
	/*
	 * 添加数据
	 */
	public int appendData(long movieId,long actorId,String roleName,float weight){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" insert into MovieActor set ");
		sqlBuff.append(" 	movieId=${movieId}, ");
		sqlBuff.append(" 	actorId=${actorId}, ");
		sqlBuff.append(" 	roleName=${roleName}, ");
		sqlBuff.append(" 	weight=${weight}, ");
		sqlBuff.append(" 	createTime=now(), ");
		sqlBuff.append(" 	updateTime=now() ");
		
		//sql参数部分
		paraHash.put("movieId",movieId);
		paraHash.put("actorId",actorId);
		paraHash.put("roleName",roleName);
		paraHash.put("weight",weight);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据ID 更新电影数据
	 */
	public int updateMovieData(long movieId,String roleName,float weight,long id){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" update MovieActor set ");
		sqlBuff.append(" 	movieId=${movieId}, ");
		sqlBuff.append(" 	roleName=${roleName}, ");
		sqlBuff.append(" 	weight=${weight}, ");
		sqlBuff.append(" 	updateTime=now() ");
		sqlBuff.append(" where id=${id} and ( ");
		sqlBuff.append(" 	movieId!=${movieId} ");
		sqlBuff.append(" 	or roleName!=${roleName} ");
		sqlBuff.append(" 	or weight!=${weight} ");
		sqlBuff.append(" ) ");
		
		//sql参数部分
		paraHash.put("movieId",movieId);
		paraHash.put("roleName",roleName);
		paraHash.put("weight",weight);
		paraHash.put("id",id);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据ID 更新演员数据
	 */
	public int updateActorData(long actorId,String roleName,float weight,long id){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" update MovieActor set ");
		sqlBuff.append(" 	actorId=${actorId}, ");
		sqlBuff.append(" 	roleName=${roleName}, ");
		sqlBuff.append(" 	weight=${weight}, ");
		sqlBuff.append(" 	updateTime=now() ");
		sqlBuff.append(" where id=${id} and ( ");
		sqlBuff.append(" 	actorId!=${actorId} ");
		sqlBuff.append(" 	or roleName!=${roleName} ");
		sqlBuff.append(" 	or weight!=${weight} ");
		sqlBuff.append(" ) ");
		
		//sql参数部分
		paraHash.put("actorId",actorId);
		paraHash.put("roleName",roleName);
		paraHash.put("weight",weight);
		paraHash.put("id",id);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据ID 更新数据
	 */
	public int updateData(long holdActorId,long actorId){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" update MovieActor set ");
		sqlBuff.append(" 	actorId=${holdActorId}, ");
		sqlBuff.append(" 	updateTime=now() ");
		sqlBuff.append(" where actorId=${actorId} ");
		
		//sql参数部分
		paraHash.put("holdActorId",holdActorId);
		paraHash.put("actorId",actorId);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据ID 删除数据
	 */
	public int deleteData(long id){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" delete from MovieActor where id=${id} ");
		
		//sql参数部分
		paraHash.put("id",id);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据演员ID 删除数据
	 */
	public int deleteDataByActorId(long actorId){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" delete from MovieActor where actorId=${actorId} ");
		
		//sql参数部分
		paraHash.put("actorId",actorId);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据电影ID 删除数据
	 */
	public int deleteDataByMovieId(long movieId){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" delete from MovieActor where movieId=${movieId} ");
		
		//sql参数部分
		paraHash.put("movieId",movieId);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 根据演员ID 获得电影演员列表
	 */
	public List<MovieActor> findMovieActorListByActorId(long actorId){
		
		//参数部分
		List<MovieActor> movieActorList = new Vector<MovieActor>();
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select MovieActor.*, ");
		sqlBuff.append(" 	Movie.name as movieName ");
		sqlBuff.append(" from MovieActor ");
		sqlBuff.append(" left join Movie on MovieActor.movieId=Movie.id ");
		sqlBuff.append(" where MovieActor.actorId=${actorId} ");
		
		//sql参数部分
		paraHash.put("actorId",actorId);
		
		//执行sql 
		movieActorList = this.dbManager.querySqlReturnVoList(sqlBuff.toString(),paraHash);
		
		//返回列表
		return movieActorList;
	}
	
	/*
	 * 根据电影ID 获得电影演员列表
	 */
	public List<MovieActor> findMovieActorListByMovieId(long movieId){
		
		//参数部分
		List<MovieActor> movieActorList = new Vector<MovieActor>();
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select MovieActor.*, ");
		sqlBuff.append(" 	Actor.name as actorName ");
		sqlBuff.append(" from MovieActor ");
		sqlBuff.append(" left join Actor on MovieActor.actorId=Actor.id ");
		sqlBuff.append(" where MovieActor.movieId=${movieId} ");
		sqlBuff.append(" order by ");
		sqlBuff.append(" 	MovieActor.weight desc, ");
		sqlBuff.append(" 	Actor.weight desc, ");
		sqlBuff.append(" 	Actor.id, ");
		sqlBuff.append(" 	MovieActor.id ");
		
		//sql参数部分
		paraHash.put("movieId",movieId);
		
		//执行sql 
		movieActorList = this.dbManager.querySqlReturnVoList(sqlBuff.toString(),paraHash);
		
		//返回列表
		return movieActorList;
	}
}
